/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Motorista.java
 *
 * Created on 10/10/2012, 21:12:28
 */

package projeto;



import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import projeto.classes.ModeloMotorista;
import projeto.classes.MotoristaAcessaDados;
import projeto.classes.Util.Util;



public class Motorista extends javax.swing.JInternalFrame {
    
    
    private ModeloMotorista m;
    private MotoristaAcessaDados mDao;
    private ArrayList<ModeloMotorista> lista;
    private br.com.fatecpg.mdi.MDI2 mdi;

    /** Creates new form Motorista */
    public Motorista() {
        initComponents();
    }
    
    public Motorista(projeto.classes.ModeloUsuario user, br.com.fatecpg.mdi.MDI2 mdi) {
        initComponents();
        this.mdi = mdi;
        
        //getUsuarioAdmin igual a 0 quer dizer que ele é um usuario normal
        if (user.getUsuarioAdmin() == 0) {
            btnExcluir.setVisible(false);
        }
        
        if (btnExcluir.isVisible()) {
            btnExcluir.setEnabled(false);
        }
        btnAlterar.setEnabled(false);
        cmbPesquisa.setEnabled(false);
        mostrarCampos(false);
        
        m = new ModeloMotorista();
        mDao = new MotoristaAcessaDados();
        
    }
    
    private void mostrarCampos(boolean flag) {
        txtNome.setEnabled(flag);
        txtCnh.setEnabled(flag);
        txtEndereco.setEnabled(flag);
        txtTelefone.setEnabled(flag);
        cmbTipoCnh.setEnabled(flag);
        cmbAtivo.setEnabled(flag);
        cmbTerceirizado.setEnabled(flag);
    }
    
    private void limparCampos() {
        txtNome.setText("");
        txtCnh.setText("");
        txtEndereco.setText("");
        txtTelefone.setText("");
        lblCodigo.setText("--");
        cmbTipoCnh.setSelectedIndex(0);
        cmbAtivo.setSelectedIndex(1);
        cmbTerceirizado.setSelectedIndex(0);
    }
    
    private void alterarLabels() {
        lblNome.setForeground(Color.black);
        lblCodigo.setForeground(Color.black);
        lblCnh.setForeground(Color.black);
        lblEndereco.setForeground(Color.black);
        lblTelefone.setForeground(Color.black);
    }
    
    private boolean validar(String nome, String cnh, String endereco, String telefone) {
        boolean valido = true;

        if (nome.length() <= 3 || nome.length() >= 30) {
            lblNome.setForeground(Color.red);
            valido = false;
        }

        if (cnh.length() != 11 || !Util.isNumber(cnh)) {
            lblCnh.setForeground(Color.red);
            valido = false;
        }

        if (endereco.length() <= 5 || endereco.length() >= 30) {
            lblEndereco.setForeground(Color.red);
            valido = false;
        }

        if (telefone.isEmpty() || !Util.isNumber(telefone)) {
            lblTelefone.setForeground(Color.red);
            valido = false;
        }

        if (!valido) {
            JOptionPane.showMessageDialog(this, "Os campos com o nome em vermelho são inválidos.\nPor favor corrija.", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } 
        
        return valido;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        lblNome = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        lblCnh = new javax.swing.JLabel();
        txtTelefone = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbTipoCnh = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cmbTerceirizado = new javax.swing.JComboBox();
        lblEndereco = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblTelefone = new javax.swing.JLabel();
        txtCnh = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cmbAtivo = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cmbOpcaoPesquisa = new javax.swing.JComboBox();
        txtConsulta = new javax.swing.JTextField();
        btnConsultar = new javax.swing.JToggleButton();
        cmbPesquisa = new javax.swing.JComboBox();
        btnNovo = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        lblCodigo = new javax.swing.JLabel();
        btnConsultarTodos = new javax.swing.JToggleButton();

        setTitle("Entrega Gado - Motorista");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("MOTORISTA");

        lblNome.setText("Nome:");

        lblCnh.setText("CNH:");

        jLabel4.setText("Tipo:");

        cmbTipoCnh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "B", "C", "D" }));

        jLabel5.setText("Ativo:");

        cmbTerceirizado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Não", "Sim" }));

        lblEndereco.setText("Endereço:");

        lblTelefone.setText("Telefone:");

        jLabel8.setText("Terceirizado:");

        cmbAtivo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Não", "Sim" }));
        cmbAtivo.setSelectedIndex(1);

        jLabel9.setText("Consultar por:");

        cmbOpcaoPesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CNH" }));

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        cmbPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbPesquisaActionPerformed(evt);
            }
        });

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        jLabel10.setText("Código:");

        lblCodigo.setText("--");

        btnConsultarTodos.setText("Todos");
        btnConsultarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(210, 210, 210)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblNome)
                            .addComponent(jLabel9)
                            .addComponent(lblCnh)
                            .addComponent(lblEndereco)
                            .addComponent(lblTelefone)
                            .addComponent(jLabel5)
                            .addComponent(jLabel8)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbOpcaoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbPesquisa, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnConsultar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnConsultarTodos))
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtCnh, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbTipoCnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbAtivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbTerceirizado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCodigo)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(66, 66, 66)
                                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConsultar)
                    .addComponent(jLabel9)
                    .addComponent(cmbOpcaoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultarTodos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(lblCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCnh)
                    .addComponent(txtCnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cmbTipoCnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEndereco)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTelefone)
                    .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cmbAtivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cmbTerceirizado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNovo)
                    .addComponent(btnSair)
                    .addComponent(btnAlterar))
                .addGap(18, 18, 18)
                .addComponent(btnExcluir)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        if (txtConsulta.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Informe um valor para consulta", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } else {
            //limpa o ArrayList
            if (lista != null) {
                lista.clear();
            }

            String filtro = "";
            if (cmbOpcaoPesquisa.getSelectedIndex() == 0) {
                //pesquisa por nome
                filtro = "nm_motorista LIKE '" + txtConsulta.getText() + "%'";
            }

            if (cmbOpcaoPesquisa.getSelectedIndex() == 1) {
                //pesquisa por combustivel
                filtro = "cd_cnh = '" + txtConsulta.getText() + "'";
            }

            try {
                lista = mDao.consulta(filtro);

                if (lista.isEmpty()) {
                    JOptionPane.showMessageDialog(this, "Não foram localizados motoristas: " + txtConsulta.getText(), "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                }

                if (lista.size() > 0) {
                    //limpa a lista
                    if (cmbPesquisa.getItemCount() > 0) {
                        cmbPesquisa.removeAllItems();
                    }
                    cmbPesquisa.setEnabled(true);
                    btnAlterar.setEnabled(true);
                    btnNovo.setText("Novo");
                    mostrarCampos(false);
                    alterarLabels();
                    if (btnExcluir.isVisible()) {
                        btnExcluir.setEnabled(true);
                    }
                    //seleciona o 1º (ou o unico) item da lista
                    ModeloMotorista modeloM = lista.get(0);
                    cmbPesquisa.addItem(modeloM.getNome());
                    cmbPesquisa.setSelectedIndex(0);
                }

                if (lista.size() > 1) {
                    //apenas inclui os outros componentes na lista (se houver)
                    for (int i = 1; i <= lista.size() - 1; i++) {
                        ModeloMotorista modeloM = lista.get(i);
                        cmbPesquisa.addItem(modeloM.getNome());
                    }
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Erro ao consultar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void cmbPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPesquisaActionPerformed
        if (evt.getSource().getClass().getSimpleName().equals("JComboBox")) {
            JComboBox cmb = (JComboBox)evt.getSource();
            int index = cmb.getSelectedIndex();
            if (index != -1) {
                ModeloMotorista modeloM = lista.get(index);
                String id = Integer.toString(modeloM.getId());
                String nome = modeloM.getNome();
                String cnh = modeloM.getCnh();
                String endereco = modeloM.getEndereco();
                int telefone = modeloM.getTelefone();
                int tipoCnh = modeloM.getCategoriaCnh();
                int ativo = modeloM.getAtivo();
                int terceirizado = modeloM.getTercerizado();
                lblCodigo.setText(id);
                txtNome.setText(nome);
                txtCnh.setText(cnh);
                txtEndereco.setText(endereco);
                txtTelefone.setText(Integer.toString(telefone));
                cmbTipoCnh.setSelectedIndex(tipoCnh);
                cmbAtivo.setSelectedIndex(ativo);
                cmbTerceirizado.setSelectedIndex(terceirizado);
            }
        }
    }//GEN-LAST:event_cmbPesquisaActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        if (btnNovo.getText().equals("Novo")) {
            mostrarCampos(true);
            limparCampos();
            alterarLabels();
            btnNovo.setText("Salvar");
            btnAlterar.setEnabled(false);
            if (btnExcluir.isVisible()) {
                btnExcluir.setEnabled(false);
            }
        } else {
            //clicou no salvar
            if (validar(txtNome.getText(), txtCnh.getText(), txtEndereco.getText(), txtTelefone.getText())) {
                m.setNome(txtNome.getText());
                m.setCnh(txtCnh.getText());
                m.setEndereco(txtEndereco.getText());
                m.setTelefone(Integer.parseInt(txtTelefone.getText()));
                m.setCategoriaCnh(cmbTipoCnh.getSelectedIndex());
                m.setAtivo(cmbAtivo.getSelectedIndex());
                m.setTercerizado(cmbTerceirizado.getSelectedIndex());
                
                try {
                    if (!mDao.incluir(m)) {
                        JOptionPane.showMessageDialog(this, "Dados gravados", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                        btnNovo.setText("Novo");
                        btnAlterar.setEnabled(true);
                        int maxId = mDao.consultaMaxId();
                        lblCodigo.setText(Integer.toString(maxId));
                        mostrarCampos(false);
                        alterarLabels();
                        if (btnExcluir.isVisible()) {
                            btnExcluir.setEnabled(true);
                        }
                    }

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao gravar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (btnAlterar.getText().equals("Alterar")) {
            mostrarCampos(true);
            alterarLabels();
            btnAlterar.setText("Salvar");
            if (btnExcluir.isVisible()) {
                btnExcluir.setEnabled(false);
            }
        } else {
            if (validar(txtNome.getText(), txtCnh.getText(), txtEndereco.getText(), txtTelefone.getText())) {
                m.setId(Integer.parseInt(lblCodigo.getText()));
                m.setNome(txtNome.getText());
                m.setCnh(txtCnh.getText());
                m.setEndereco(txtEndereco.getText());
                m.setTelefone(Integer.parseInt(txtTelefone.getText()));
                m.setCategoriaCnh(cmbTipoCnh.getSelectedIndex());
                m.setAtivo(cmbAtivo.getSelectedIndex());
                m.setTercerizado(cmbTerceirizado.getSelectedIndex());

                try {
                    if (mDao.alterar(m) > 0) {
                        JOptionPane.showMessageDialog(this, "Dados alterados", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                        btnAlterar.setText("Alterar");
                        mostrarCampos(false);
                        if (btnExcluir.isVisible()) {
                            btnExcluir.setEnabled(true);
                        }
                    }

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao gravar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if (lblCodigo.getText().equals("--")) {
            JOptionPane.showMessageDialog(this, "Registro inválido", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
            lblCodigo.setForeground(Color.red);
        } else {
            try {
                int id = Integer.parseInt(lblCodigo.getText());

                if (mDao.deletar(id) == 1) {
                    JOptionPane.showMessageDialog(this, "Registro excluido", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                    limparCampos();
                    mostrarCampos(false);
                    alterarLabels();
                    btnExcluir.setEnabled(false);
                    btnAlterar.setEnabled(false);
                }

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Erro ao excluir " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnConsultarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarTodosActionPerformed
        Motorista_ConsultaTodos mTodos = new Motorista_ConsultaTodos(mdi, true);
        mTodos.setVisible(true);
    }//GEN-LAST:event_btnConsultarTodosActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JToggleButton btnConsultar;
    private javax.swing.JToggleButton btnConsultarTodos;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSair;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmbAtivo;
    private javax.swing.JComboBox cmbOpcaoPesquisa;
    private javax.swing.JComboBox cmbPesquisa;
    private javax.swing.JComboBox cmbTerceirizado;
    private javax.swing.JComboBox cmbTipoCnh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblCnh;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblEndereco;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTelefone;
    private javax.swing.JTextField txtCnh;
    private javax.swing.JTextField txtConsulta;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtTelefone;
    // End of variables declaration//GEN-END:variables

}
